package com.example.hotel.web;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Roomtype;
import com.example.hotel.service.RoomtypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "获取房间类型信息")
@RestController
@RequestMapping("roomtype")
public class RoomtypeController {
    @Autowired
    RoomtypeService roomtypeService;
    @Autowired
    private Roomtype roomtype;



    @ApiOperation(value = "查询房间所有类型")
    @GetMapping("findAll")
    public JsonResult findAllRoomtype() {
        try {
            List<Roomtype> rooms= roomtypeService.findAll();
            if (!rooms.isEmpty()) {
                return JsonResult.ok(rooms);
            } else {
                return JsonResult.fail(5001, "房间类型查询失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();}
    }


    @ApiOperation(value = "添加房间类型")
    @PostMapping("add")
    public JsonResult addRoomtype(Roomtype roomtype) {
        try {
            int r = roomtypeService.insert(roomtype);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "房间类型添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "删除房间类型")
    @PostMapping("delete")
    public JsonResult deleteDishtype(Integer roomtypeId) {
        try {
            if (roomtypeId == null) {
                return JsonResult.fail(5001, "房间类型id不能为空");
            }
            int r = roomtypeService.deleteById(roomtypeId);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "房间类型删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "更新房间类型")
    @PostMapping("update")
    public JsonResult updateRoomtype(@ApiParam(name = "roomtypeId", value = "1", required = true)Integer roomtypeId, @ApiParam(name = "roomtypeName", value = "1", required = true)String roomtypeName, @ApiParam(name = "capacity", value = "1", required = true)String capacity, @ApiParam(name = "description", value = "1", required = true)String description) {
        try {
            int r = roomtypeService.update(roomtypeId,roomtypeName,capacity,description);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "房间类型更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }

    }



}
